<template>
  <div id="payments">
    <div class="card">
      <div class="card-header">
        <h4>付款方式</h4>
        <span>向供应商支付时所使用的支付途径</span>
      </div>
      <div class="card-body">
        <form class="row">
          <div class="col-1">
            <select
              class="form-select ms-1"
              name="sc.status"
              v-model.number="scStatus"
            >
              <option value="-1">状态</option>
              <option value="0">停用</option>
              <option value="1">启用</option>
            </select>
          </div>
          <div class="col">
            <button
              type="button"
              class="btn btn-primary ms-1"
              v-on:click="search()"
            >
              查找
            </button>
            <a
              href="javascript:void(0)"
              class="btn btn-success ms-auto float-end"
              v-on:click="newPayment()"
              >新建</a
            >
          </div>
        </form>
      </div>
    </div>
    <table class="table table-striped table-sm">
      <thead>
        <tr>
          <th>名称</th>
          <th>拼音</th>
          <th>开户行</th>
          <th>开户名</th>
          <th>账号</th>
          <th>状态</th>
          <th>创建日期</th>
          <th>修改日期</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <template v-for="(info, index) in dataList">
          <tr :key="`pm_` + info.id">
            <td>{{ info.name }}</td>
            <td>{{ info.pinyin }}</td>
            <td>{{ info.bankName }}</td>
            <td>{{ info.accountName }}</td>
            <td>{{ info.accountNo }}</td>
            <td>
              <template v-if="info.status">
                <span class="text-success text-lg">启用</span>
              </template>
              <template v-else>
                <span class="text-danger">停用</span>
              </template>
            </td>
            <td>{{ info.createTime }}</td>
            <td>{{ info.lastUpdate }}</td>
            <td>
              <button
                class="btn btn-info btn-sm"
                @click.stop="editPaymentMethod(index)"
              >
                修改
              </button>
            </td>
          </tr>
          <tr
            :key="`pm_remark_` + info.id"
            v-if="info.remark !== null && info.remark.length > 0"
          >
            <td></td>
            <td colspan="8">备注：{{ info.remark }}</td>
          </tr>
        </template>
      </tbody>
    </table>
    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>

    <my-modal-payment-method ref="pmModal"></my-modal-payment-method>
  </div>
</template>

<script>
import MyPagination from "@/components/my-pagination.vue";
import $ from "jquery";
import { searchPaymentMethods, savePaymentMethod } from "@/api/basic-data.js";
import MyModalPaymentMethod from "@/components/modal/my-modal-payment-method.vue";

export default {
  components: {
    MyPagination,
    MyModalPaymentMethod,
  },
  data() {
    return {
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 15,
        pageTotal: 0,
      },
      scStatus: -1,

      id: 0,
      name: "",
      pinyin: "",
      bankName: "",
      accountName: "",
      accountNo: "",
      remark: "",
      status: 1,

      dataList: [],
    };
  },
  mounted: function () {
    if (window.matchMedia("(max-width: 576px)").matches) {
      this.$router.replace("/h5/paymentTypes");
    } else {
      this.search();
    }
  },
  methods: {
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", { loading: false });
    },
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      this.showLoading();

      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.status": this.scStatus
      };

      searchPaymentMethods(
        params,
        (v) => {
          this.dataList = v.dataList;
          this.sc = v.page;
        },
        () => {
          this.hideLoading();
        }
      );
    },
    reset: function () {
      this.id = 0;
      this.name = "";
      this.pinyin = "";
      this.bankName = "";
      this.accountName = "";
      this.accountNo = "";
      this.remark = "";
      this.status = 1;
    },
    newPayment: function () {
      this.reset();

      const o = this;
      const params = {
        id: o.id,
        name: o.name,
        pinyin: o.pinyin,
        bankName: o.bankName,
        accountName: o.accountName,
        accountNo: o.accountNo,
        remark: o.remark,
        status: o.status,
      };

      this.$refs.pmModal.modal(params).then((v) => this.save(v));
    },
    editPaymentMethod: function (idx) {
      const o = this.dataList[idx];

      const params = {
        id: o.id,
        name: o.name,
        pinyin: o.pinyin,
        bankName: o.bankName,
        accountName: o.accountName,
        accountNo: o.accountNo,
        remark: o.remark,
        status: o.status,
      };

      this.$refs.pmModal.modal(params).then((v) => this.save(v));
    },
    save: function (params) {
      savePaymentMethod(params, (jsonResult) => {
        if (jsonResult.status === "OK") {
          this.search();
          this.showErrMsg("操作成功");
        } else {
          this.showErrMsg("操作失败：" + jsonResult.errmsg, "danger");
        }
      });
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
